<?php
//ref:http://www.webmonkey.com/tutorial/Get_Started_With_the_Yahoo_HTTP_Geocoder_API

     $appid = "dj0yJmk9T1dueGFoZU1nVzluJmQ9WVdrOVpHSXlZVEpxTm1VbWNHbzlNakEyTnpReU9UVTRNUS0tJnM9";
     $bossNewsUrl = "http://boss.yahooapis.com/ysearch/news/v1/election%20new%20jersey%20governor?appid=$appid&count=50&format=xml&style=raw&abstract=long";

     $bossWebUrl = "http://boss.yahooapis.com/ysearch/web/v1/election%20new%20jersey%20governor?appid=dj0yJmk9T1dueGFoZU1nVzluJmQ9WVdrOVpHSXlZVEpxTm1VbWNHbzlNakEyTnpReU9UVTRNUS0tJnM9&count=50&format=xml&view=keyterms,delicious_toptags&style=raw&abstract=long";

     //from news search
     if (!$xml = file_get_contents($bossNewsUrl)) {

           echo "It didn't work!\n";

     } else {

                        // Let's use the SimpleXML to drop the whole XML
                        // output into an object we can later interact with easilly
      $xmlObj = new SimpleXMLElement($xml, LIBXML_NOCDATA);
     // echo "Got the XML<br />";
     }
     $res = $xmlObj->resultset_news->result[0]->url;

     foreach($xmlObj->resultset_news->result as $res)
     {
	  //echo 'result: '.$res->abstract. ' end.<br />';
	  $result_string .= $res->title .' ';
	  $result_string .= $res->abstract.' ';

     }
     /******************* Tag cloud stuff ******************************/
// Store frequency of words in an array
$freqData = array();

// Get individual words and build a frequency table
foreach(str_word_count( $result_string, 1 ) as $word )
{
	 // $word = strtolower($word);//eliminate caps
	if(strlen($word) > 2 && strcmp($word, 'the') && strcmp($word, 'and') && strcmp($word, 'is') && strcmp($word, 'his')&& strcmp($word, 'for')){
	// For each word found in the frequency table, increment its value by one
	array_key_exists( $word, $freqData ) ? $freqData[ $word ]++ : $freqData[ $word ] = 1;
	}
}
//eliminate low counts
foreach($freqData as $key => $count)
{
     if($count < 2) unset($freqData[$key]);
}

     //************************ web search keywords *************************************
     $keywords = array();

      if (!$webXml = file_get_contents($bossWebUrl)) {

           echo "Websearch didn't work!\n";

     } else {
      $webXmlObj = new SimpleXMLElement($webXml, LIBXML_NOCDATA);
     // echo "Got the web search<br />";
     }

     foreach($webXmlObj->resultset_web->result as $res)
     {
	  foreach($res->keyterms->terms->term as $keywd)
	  {
	//  echo 'keyword: '.$keywd. '<br />';
	  $keywords[] = (string)$keywd;
	  }
	  if(count($res->delicious_toptags->tags) > 0){
	       foreach($res->delicious_toptags->tags->tag as $tag)
	       {
		   // echo 'tag: '. $tag. '<br />';
		    $keywords[]= (string)$tag;
	       }
	  }
     }


/******************* Tag cloud stuff ******************************/
// Store frequency of words in an array
$webFreqData = array();

// Get individual words and build a frequency table
foreach($keywords as $word )
{
     array_key_exists( $word, $webFreqData ) ? $webFreqData[ $word ]++ : $webFreqData[ $word ] = 1;

}
// //eliminate low counts
foreach($webFreqData as $key => $count)
{
     if($count < 2) unset($webFreqData[$key]);
}

// ==============================================================
// = Function to actually generate the cloud from provided data =
// ==============================================================
function getCloud( $data = array(), $minFontSize = 12, $maxFontSize = 50 )
{
	$minimumCount = min( array_values( $data ) );
	$maximumCount = max( array_values( $data ) );
	$spread       = $maximumCount - $minimumCount;
	$cloudHTML    = '';
	$cloudTags    = array();

	$spread == 0 && $spread = 1;

	foreach( $data as $tag => $count )
	{
		$size = $minFontSize + ( $count - $minimumCount )
			* ( $maxFontSize - $minFontSize ) / $spread;
		$cloudTags[] = '<a style="font-size: ' . floor( $size ) . 'px'
		. '" class="tag_cloud" href="http://www.google.com/search?q=' . $tag
		. '" title="\'' . $tag  . '\' returned a count of ' . $count . '">'
		. htmlspecialchars( stripslashes( $tag ) ) . '</a>';

	}

	return join( "\n", $cloudTags ) . "\n";
}
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
	"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
	<title>Tag Cloud Demo</title>
	<style type="text/css" media="screen">
		/*<![CDATA[*/
		.tag_cloud { padding: 3px; text-decoration: none; }
		.tag_cloud:link  { color: #81d601; }
		.tag_cloud:visited { color: #019c05; }
		.tag_cloud:hover { color: #ffffff; background: #69da03; }
		.tag_cloud:active { color: #ffffff; background: #ACFC65; }
		/*]]>*/
	</style>
</head>

<body>
	<h1>Web Search Keyterms Tag Cloud</h1>
	<h3>Search terms: election new jersey governor</h3>
	<div id="wrapper">
		<?php echo getCloud( $webFreqData ) ?>
	</div>
	<hr>
	<h1>News Search Abstract Tag Cloud</h1>
	<h3>Search terms: election new jersey governor</h3>
	<div id="wrapper">
		<?php echo getCloud( $freqData ) ?>
	</div>

</body>
</html>

